//
//  YFThreeDViewController.m
//  SquashSpark
//
//  Created by Coollang on 2017/12/18.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFThreeDViewController.h"
#import "UnityAppController.h"
#import "NewCurveLineChartView.h"
#import "KLSoundplay.h"
#import "YFSearchController.h"
#import "JCDropTableViewController.h"
#import "NSString+Extension.h"
#import "YFSportAndSensorApi.h"


@interface YFThreeDViewController ()<JCDropTableViewDelegate,JCDropTableViewDataSource>

@property (weak, nonatomic) UIView *unity3DView;
@property (strong, nonatomic) IBOutlet UIView *unityWindow;
@property (weak, nonatomic) IBOutlet UIButton *connectButton;

@property (weak, nonatomic) IBOutlet UILabel *powerLabel;
@property (weak, nonatomic) IBOutlet UILabel *swingSpeedLabel;
@property (weak, nonatomic) IBOutlet UILabel *swingTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *powerUnitLabel;
@property (weak, nonatomic) IBOutlet UILabel *SwingSpeedUnitLabel;
@property (weak, nonatomic) IBOutlet UILabel *swingTimeUnitLabel;

@property (weak, nonatomic) IBOutlet UIView *lineSuperView;
@property (weak, nonatomic) IBOutlet UIView *maskView;
// 曲线
@property (weak, nonatomic) IBOutlet NewCurveLineChartView *curveLineView;
/** 声音开关 */
@property (nonatomic, strong)UIBarButtonItem *soundBarItem;
// 曲线图底部约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *curveLineBottomCon;
/** 连接按钮高度约束 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *connectBtnHeightCon;
/** 曲线父视图高度约束 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineSpuerViewHeightCon;

/** 当前挥拍的3d数据模型 */
@property (nonatomic, strong) YFThreeDItem *currentThreeDItem;

/** 时候正在显示当前控制器视图 */
@property (nonatomic, assign)BOOL isShowCurrentView;

/** 点击显示或隐藏曲线图 */
@property (nonatomic, assign)BOOL tapShowCurveLineView;

/** 是否模板需要保存 */
@property (nonatomic, assign) BOOL haveTemplateNeedSave;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *curveLineSuperViewBottomCon;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *unity3DBottom;

@property (nonatomic, strong) NSMutableArray *array;

/** 球的类型 */
@property (nonatomic, strong) NSArray *dropActionTypes;

@end
@implementation YFThreeDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addUnity3DView];
    [self confiuerUpUI];
    // 注册通知
    [self regiesterNotification];
}
- (IBAction)replay:(id)sender {
    
}
- (void)updateBtnAndCurveLine {
    // 是否显示按钮
    BOOL isHiddenBtn = NO;
    BOOL isHiddenCurveLine = NO; // 是否影藏曲线图
    BOOL isCurveHigh = NO;
    if ([YFCommunicationManager shareInstance].isPeripheralConnected) {
        if (self.type == kThreeDRealTypePractice) {
            isHiddenBtn = YES;
            //isHiddenCurveLine = NO;
            isHiddenCurveLine = !self.tapShowCurveLineView;
            isCurveHigh = NO;
        } else if (self.type == kThreeDRealTypeRecordTemplate){
            [self.connectButton setTitle:@"SAVE ACTION MODEL" forState:UIControlStateNormal];
            self.connectButton.titleLabel.font = kOpenSansFont(OpenSansExtraBold, 12);
            if (self.currentThreeDItem != nil && self.haveTemplateNeedSave == YES) {
                // isHiddenCurveLine = NO;
                isHiddenCurveLine = !self.tapShowCurveLineView;
                isCurveHigh = YES;
                isHiddenBtn = NO;
            }else {
                isHiddenBtn = YES;
                // isHiddenCurveLine = NO;
                isHiddenCurveLine = !self.tapShowCurveLineView;
                isCurveHigh = NO;
            }
        }
    }else {
        [self.connectButton setTitle:@"CONNECTION DEVICE" forState:UIControlStateNormal];
        self.connectButton.titleLabel.font = kOpenSansFont(OpenSansExtraBold, 16);
        isHiddenBtn = NO;
        isHiddenCurveLine = YES;
        isCurveHigh = YES;
    }
    __weak typeof(self) weakSelf = self;
    self.curveLineBottomCon.constant = isCurveHigh ? 65:20;
    self.connectBtnHeightCon.constant = isHiddenBtn ? 0:45;
    self.lineSpuerViewHeightCon.constant = ((220 - 65) + self.curveLineBottomCon.constant);
    [UIView animateKeyframesWithDuration:0.35 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        weakSelf.lineSuperView.alpha = !isHiddenCurveLine;
        weakSelf.maskView.alpha = !isHiddenCurveLine;
        weakSelf.connectButton.alpha = !isHiddenBtn;
        [weakSelf.lineSuperView layoutIfNeeded];
        [weakSelf.connectButton layoutIfNeeded];
        [weakSelf.maskView layoutIfNeeded];
        [weakSelf.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //[[UIApplication sharedApplication] setIdleTimerDisabled:YES];///屏幕长亮
    // 进入3D
    self.isShowCurrentView = YES;
    [self performSelector:@selector(enter3DMode) withObject:nil afterDelay:0.5];
    if (!_unity3DView) {
        [self addUnity3DView];
    }else{
        _unity3DView.frame = _unityWindow.bounds;
        [_unityWindow addSubview:_unity3DView];
    }
    [self updateBtnAndCurveLine];
    if ([YFUserDefaults generalMetricSystemUnit]) {
        self.SwingSpeedUnitLabel.text = @"Best Results(kph)";
    }else{
        self.SwingSpeedUnitLabel.text = @"Best Results Speed(mph)";
    }
}


- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    self.isShowCurrentView = NO;
    [self quitRealTimeMode];
    [self removeUnity3DView];
}
- (void)dealloc{
    YFLog(@"释放practice");
}
- (void)confiuerUpUI {
    if (self.type == kThreeDRealTypePractice) {
        self.navigationItem.title = @"Real Time 3D";
    }else {
        self.navigationItem.title = @"Create Model";
    }
    [self.connectButton clipsCornerRadius:3];
    self.curveLineSuperViewBottomCon.constant = kDevice_Is_iPhoneX?(45+35):45;
    self.unity3DBottom.constant = kDevice_Is_iPhoneX?35:0;
    NSString *soundImageName = [KLSoundplay shareSpeedSoundplayer].shouldSound ? @"home_open_voice":@"home_close_voice";
    self.soundBarItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:soundImageName] style:(UIBarButtonItemStyleDone) target:self action:@selector(setVoiceBarItemAction:)];
    self.navigationItem.rightBarButtonItem = self.soundBarItem;
    // 字体适配
    UIFont *unitFont = kOpenSansFont(OpenSansRegular, 11);
    UIFont *dataFont = kOpenSansFont(OpenSansRegular, 27);
    self.powerUnitLabel.font = unitFont;
    self.SwingSpeedUnitLabel.font = unitFont;
    self.SwingSpeedUnitLabel.font = unitFont;
    self.powerLabel.font = dataFont;
    self.swingSpeedLabel.font = dataFont;
    self.swingTimeLabel.font = dataFont;
    [self updateBtnAndCurveLine];
    
    if (self.type != kThreeDRealTypeRecordTemplate) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"connectSensorRightItem"] style:UIBarButtonItemStyleDone target:self action:@selector(connectSensor:)];
        self.soundBarItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:soundImageName] style:(UIBarButtonItemStyleDone) target:self action:@selector(setVoiceBarItemAction:)];
        self.navigationItem.leftBarButtonItem = self.soundBarItem;
    }
}

- (void)connectSensor:(UIBarButtonItem *)sender{
    [YFSensorManager openSensorControllrWithVc:self isNecessaryOpenSensorVc:NO];
}

- (void)setVoiceBarItemAction:(UIBarButtonItem *)item {
    [KLSoundplay shareSpeedSoundplayer].shouldSound = ![KLSoundplay shareSpeedSoundplayer].shouldSound;
    NSString *soundImageName = [KLSoundplay shareSpeedSoundplayer].shouldSound ? @"home_open_voice":@"home_close_voice";
    item.image = [UIImage imageNamed:soundImageName];
}

- (IBAction)connectButtonClick:(UIButton *)sender {
    if ([YFCommunicationManager shareInstance].isCentralReady && ![YFCommunicationManager shareInstance].isPeripheralConnected) {
        [self openSearchDevice];
    }else {
        //保存模板
        if (self.type == kThreeDRealTypeRecordTemplate && self.haveTemplateNeedSave == YES) {
            
            __weak __typeof(self)weakSelf = self;
            JCDropTableViewController *_dropView = [JCDropTableViewController new];
            _dropView.dataSource = self;
            _dropView.delegate = self;
            _dropView.view.frame = self.view.bounds;
            [self addChildViewController:_dropView];
            [self.view addSubview:_dropView.view];
            [self quitRealTimeMode];
            _dropView.dismissBlock = ^(BOOL isConform){
                if (isConform) {
                    [weakSelf dismissViewControllerAnimated:YES completion:nil];
                    [weakSelf.hud showSuccessWithStatus:@"Save Action Model Success" dimissAfterSecond:1.5];
                    [weakSelf enter3DMode];
                }else{
                    [weakSelf enter3DMode];
                }
            };
            [_dropView presentView];
          
//            [self creatTemplateWithType:TemplateMainTypeMy withThreeItem:self.currentThreeDItem];
        }
    }
}
#pragma mark - 蓝牙搜索控制器
- (void)openSearchDevice {
//    if ([YFCommunicationManager shareInstance].isCentralReady && [YFCommunicationManager shareInstance].isPeripheralConnected == NO) {
//        YFSearchController *searchVc = [[UIStoryboard storyboardWithName:@"Sensor" bundle:nil] instantiateViewControllerWithIdentifier:@"YFSearchController"];
//        searchVc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:searchVc animated:YES];
//    }
    [YFSensorManager openSensorControllrWithVc:self isNecessaryOpenSensorVc:YES];
}

#pragma mark - 蓝牙数据
- (void)enter3DMode {
    __weak typeof(self) weakSelf = self;
    [[YFCommunicationManager shareInstance] enter3DModel:^(BOOL success, NSError *error) {
        
    } swing3DDataCallBlock:^(YFThreeDItem *threeDitem, NSError *error) {
        
        weakSelf.tapShowCurveLineView = YES;
        weakSelf.currentThreeDItem = threeDitem;
//        if (_array == nil) {
//            _array = [NSMutableArray array];
//        }
//        [_array addObject:[threeDitem threeDItemToDictionary]];
//
//        if (_array.count == 5) {
//            NSString *plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject stringByAppendingPathComponent:@"SystemModel.txt"];
//            BOOL suc = [[NSString dictionaryToJson:_array] writeToFile:plistPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
////            BOOL suc = [[NSString dictionaryToJson:_array] writeToFile:plistPath atomically:YES];
//            NSLog(@"%zd",suc);
//        }
        //播放语音
        if ([KLSoundplay shareSpeedSoundplayer].shouldSound) {
            if ([YFUserDefaults generalMetricSystemUnit]) {
                [[KLSoundplay shareSpeedSoundplayer] soundTheSpeed:threeDitem.swingSpeed];
            }else {
                [[KLSoundplay shareSpeedSoundplayer] soundTheSpeed:round([Common kmhToMph:threeDitem.swingSpeed])];
            }
        }
    }];
}

- (void)quitRealTimeMode {
    [[YFCommunicationManager shareInstance] quitRealTimeModelCompleted:^(BOOL success) {
        
    }];
}
- (void)setCurrentThreeDItem:(YFThreeDItem *)currentThreeDItem {
    _currentThreeDItem = currentThreeDItem;
    [self configureLineChartViewWithThreeDItem:currentThreeDItem];
    [self configureDataViewWithThreeDItem:currentThreeDItem];
    
    if (self.type == kThreeDRealTypeRecordTemplate) {
        self.haveTemplateNeedSave = YES;
    }
    NSDictionary *threeDDictionary = @{
                                       @"data": [currentThreeDItem getValidThreeDDetailItemsData]
                                       };
    
    NSString *threeDJsonString = [self threeDModeJsonStringWithDictionary:threeDDictionary];
    //    NSString *path = [[NSBundle mainBundle] pathForResource:@"Data.txt" ofType:nil];
    //    threeDJsonString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [self U3dTrainingWithJsonString:threeDJsonString];
}
- (void)setHaveTemplateNeedSave:(BOOL)haveTemplateNeedSave {
    _haveTemplateNeedSave = haveTemplateNeedSave;
    [self updateBtnAndCurveLine];
}

- (NSString *)threeDModeJsonStringWithDictionary:(NSDictionary *)threeDItemDictionary {
    NSError *error;
    NSData *threeDItemsData = [NSJSONSerialization dataWithJSONObject:threeDItemDictionary options:NSJSONWritingPrettyPrinted error:&error];
    NSString *threeDItemsString = [[NSString alloc] initWithData:threeDItemsData encoding:NSUTF8StringEncoding];
    if (!error) {
        return threeDItemsString;
    }
    return nil;
}

#pragma mark - 通知
- (void)regiesterNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceConnectHandel) name:kLGPeripheralDidConnect object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceDidDisconnectHandle) name:kLGPeripheralDidDisconnect object:nil];
}
- (void)deviceConnectHandel {
    if (self.isShowCurrentView) {
        [self enter3DMode];
    }
    [self updateBtnAndCurveLine];
}

- (void)deviceDidDisconnectHandle {
    [self updateBtnAndCurveLine];
}

#pragma mark - 配置底部曲线图和数据
- (void)configureDataViewWithThreeDItem:(YFThreeDItem *)threeDItem {
    self.powerLabel.text = [NSString stringWithFormat:@"%0.f",threeDItem.strength];
    self.swingTimeLabel.text = @(threeDItem.swingTime).stringValue;
    if ([YFUserDefaults generalMetricSystemUnit]) {
        self.swingSpeedLabel.text = [NSString stringWithFormat:@"%zd",threeDItem.swingSpeed];
    }else {
        self.swingSpeedLabel.text = [NSString stringWithFormat:@"%0.f",round([Common kmhToMph:threeDItem.swingSpeed])];
    }
}
- (void)configureLineChartViewWithThreeDItem:(YFThreeDItem *)threeDItem {
    // 1.计算力度
    NSMutableArray *powerArrM = [NSMutableArray array];
    NSMutableArray *speedArrM = [NSMutableArray array];
    NSMutableArray *xAxisValues = [NSMutableArray array];
    NSInteger maxSpeed = 0;
    NSInteger maxStrength = 0;
    
    // 找到最大的力度
    for (YFThreeDDetailItem *detailItem in threeDItem.threeDDetailItems) {
        [powerArrM addObject:detailItem.strength];
        [speedArrM addObject:[detailItem speed]];
        if ([detailItem speed].integerValue > maxSpeed) {
            maxSpeed = [detailItem speed].integerValue ;
        }
        if ([detailItem strength].floatValue > maxStrength) {
            maxStrength = [detailItem strength].floatValue;
        }
        [xAxisValues addObject:@(detailItem.indexI)];
    }
    
    if (maxStrength > 0) {
        maxStrength = maxStrength + 1;//(maxStrength - maxStrength % 10);
    }else {
        maxStrength = 10;
    }
    if (maxSpeed > 0 && maxSpeed / 10 != 0) {
        maxSpeed = (maxSpeed - maxSpeed % 10) + 10;
    }else {
        maxSpeed = 200;
    }
    //    NSLog(@"%@-%@",powerArrM,speedArrM);
    [self.curveLineView ConfigerTheme:kSquashThreeDStyle];
    // 计算最大值和最小值
    [self.curveLineView defaultSuitableSettingCompare:0 maxValue:maxStrength setAxis:kAxisLeft labelCount:5 show:YES showLabel:YES drawGridLinesEnabled:NO axisLineColor:[UIColor colorWithHex:0x354F6E]];
    [self.curveLineView defaultSuitableSettingCompare:0 maxValue:maxSpeed setAxis:kAxisRight labelCount:8 show:YES showLabel:YES drawGridLinesEnabled:YES axisLineColor:[UIColor colorWithHex:0x354F6E]];
    
    CurveLineItem *linePowerItem = [[CurveLineItem alloc] initWithLineWidth:2 color:kGreenLineColor fullFormColor:[UIColor colorWithHex:0x0ecd7e alpha:0.4] fullToColor:[UIColor colorWithHex:0x0ecd7e alpha:0.1] xValues:xAxisValues.copy yValues:powerArrM.copy];
    linePowerItem.yAxisDependency = AxisDependencyLeft;
    CurveLineItem *lineSpeedItem = [[CurveLineItem alloc] initWithLineWidth:2 color:kOrgangeLineColor fullFormColor:[UIColor colorWithHex:0xfa9900 alpha:0.4] fullToColor:[UIColor colorWithHex:0xfa9900 alpha:0.1] xValues:xAxisValues.copy yValues:speedArrM.copy];
    lineSpeedItem.yAxisDependency = AxisDependencyRight;
    [self.curveLineView drawLineWithCurveLineItemArr:@[linePowerItem,lineSpeedItem] showXAnimation:NO showYAnimation:YES];
    // 配置X轴
    // [self.curveLineView configuerXAxisValus:@[]];
}

#pragma mark - 创建和移除unity3D视图
// 加载unity3D视图
- (void)addUnity3DView{
    if (_unity3DView == nil) {
        _unity3DView = UnityGetGLView();
        _unity3DView.frame = _unityWindow.bounds;
        [_unityWindow addSubview:_unity3DView];
        [self U3dTraningMode];
        [self U3dReturnToSingle];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self U3dSetLanguage];
            [self U3dSetTrailSpeedColor];
            [self U3dSingleShowKeyPoint];
            // 添加点击事件
            UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapThreeDViewAction:)];
            [self.unity3DView addGestureRecognizer:tapGes];
        });
    }
}
- (void)tapThreeDViewAction:(UITapGestureRecognizer *)tapGes {
    CGPoint tapPoint = [tapGes locationInView:self.unity3DView];
    if (tapPoint.y < self.unity3DView.bounds.size.height - 45) {
        self.tapShowCurveLineView = !self.tapShowCurveLineView;
        
    }
}
- (void)setTapShowCurveLineView:(BOOL)tapShowCurveLineView {
    BOOL oldValue = _tapShowCurveLineView;
    _tapShowCurveLineView = tapShowCurveLineView;
    if (oldValue != tapShowCurveLineView) {
        [self updateBtnAndCurveLine];
    }
}
// 移除unity3D视图
- (void)removeUnity3DView{
    if (_unity3DView) {
        [_unity3DView removeFromSuperview];
        _unity3DView = nil;
    }
}

#pragma mark -  jc dorpVc代理
- (NSInteger)numberOfRowsInHandType:(LeftHandType)leftHandType {
    if (leftHandType == LeftHandTypeUnknow) {
        return 0;
    }else if (leftHandType == LeftHandTypeFore) {///正手
        return self.dropActionTypes.count;
    }else{///反手
        return self.dropActionTypes.count;
    }
}
- (NSString *)textForCellInHandType:(LeftHandType)leftHandType row:(NSInteger)row{
    
    return [SquashEnum stringWithSportActionType:[self.dropActionTypes[row] integerValue]];
}
- (NSArray *)dropActionTypes {
    if (_dropActionTypes == nil) {
        _dropActionTypes = @[@(kSportActionDrop),@(kSportActionLop),@(kSportActionLength),@(kSportActionBoast),@(kSportActionOther)];
    }
    return _dropActionTypes;
}

- (void)conformActionForInputText:(NSString *)inputText selectedIndexPath:(NSIndexPath *)indexPath {
    // 模板名称
    inputText = [inputText isEqualToString:@""]?@"Unnamed" : inputText;
    HandDirectionType handType = indexPath.section == -1 ? HandDirectionTypeForeward:(indexPath.section == 0 ? HandDirectionTypeForeward:HandDirectionTypeBackward);
    kSportActionType actionType = indexPath.row % 5;
    [self creatTemplateWithType:TemplateMainTypeMy withThreeItem:self.currentThreeDItem name:inputText handType:handType actionType:actionType];
}

// 创建模板
- (void)creatTemplateWithType:(TemplateMainType)type withThreeItem:(YFThreeDItem *)threeDItem name:(NSString *)name handType:(HandDirectionType)selectHandType actionType:(kSportActionType)selectActionType {
    
    NSString *imageName;
    NSArray *imageNames = @[@"吊球",@"挑球",@"长球",@"侧墙球",@"其他"];
    imageName = imageNames[selectActionType];
    YFThreeDTemplate *template = [[YFThreeDTemplate alloc] initWithThreeDitem:threeDItem name:name templateImage:imageName mainType:type];
    template.selectActionType = selectActionType;
    template.handType = selectHandType;
    [YFThreeDTemplate saveTemplateToDocument:template];
    if ([self.delegate respondsToSelector:@selector(didSaveRecordTemplateWithTemplateItem:)]) {
        [self.delegate didSaveRecordTemplateWithTemplateItem:template];
    }
    self.haveTemplateNeedSave = NO;
    //模板上传
    [YFSportAndSensorApi addTemplateToServer:template completion:^(NSString *tempaleID, NSError *error) {
        if (error) {
            
        }else{
            [[RLMRealm defaultRealm] transactionWithBlock:^{
                template.templateID = tempaleID;
            }];
        }
    }];
}


#pragma mark - unity3D配置
//设置拖尾最大速度
-(void) U3dSetTrailMaxSpeed
{
    //参数为int 转成字符串
    UnitySendMessage("MsgReceiver","U3dSetTrailMaxSpeed","200");
}
//设置语言
-(void) U3dSetLanguage
{
    // _language 0中文1英文  unity启动1s-2s后发送
    int _language = 1;
    NSString* _string = [NSString stringWithFormat:@"%d", _language];
    const char * _s =[_string UTF8String];
    UnitySendMessage("MsgReceiver","U3dSetLanguage",_s);
}
-(void) U3dSendAnimationData:(int) _type OffSet:(int) _offset
{
    NSString* _string = [NSString stringWithFormat:@"%d|%d", _type, _offset];
    const char * _s =[_string UTF8String];
    UnitySendMessage("MsgReceiver","U3dPlayAnimation",_s);
}
-(void) U3dPlayRange:(int) _start End:(int) _end//范围播放
{
    NSString* _string = [NSString stringWithFormat:@"%d|%d", _start, _end];
    const char * _s =[_string UTF8String];
    UnitySendMessage("MsgReceiver","SetPlayRange",_s);
}
-(void) U3dSetKeyPoint:(int) _progress Type:(int) _type//设置  最高速度点，击球点   参数1范围0-100， 参数2:0击球点,1最大速度,2示范击球点,3示范最大速度，
{
    NSString* _string = [NSString stringWithFormat:@"%d|%d", _progress, _type];
    const char * _s =[_string UTF8String];
    UnitySendMessage("MsgReceiver","SetKeyPoint",_s);
}
-(void) U3dPlayDemo//示范
{
    UnitySendMessage("MsgReceiver","ShowDemoPlay","");
}
-(void) U3dShowAngel:(int) _angel//夹角 放大100倍
{
    NSString* _string = [NSString stringWithFormat:@"%d", _angel];
    const char * _s =[_string UTF8String];
    UnitySendMessage("MsgReceiver","ShowAngel",_s);
}
-(void) U3dSetSlowState//slow慢速每点一次slow，normal切换
{
    UnitySendMessage("MsgReceiver","SetSlowState","");
}
-(void) U3dSetRepeatState//repeat//，重复 每点一次repeat，once切换
{
    UnitySendMessage("MsgReceiver","SetRepeatState","");
}

//设置拖尾颜色
-(void) U3dSetTrailSpeedColor
{
    // 数据格式index,r,g,b,a； index为int 0min,1mid,2max ,rgba为0-255
    UnitySendMessage("MsgReceiver","U3dSetTrailSpeedColor","0,0,255,240,255");
    UnitySendMessage("MsgReceiver","U3dSetTrailSpeedColor","1,217,255,16,255");
    UnitySendMessage("MsgReceiver","U3dSetTrailSpeedColor","2,255,86,0,255");
}
//是否显示关键点
-(void) U3dSingleShowKeyPoint
{
    //参数为string "true" “false”
    UnitySendMessage("MsgReceiver","U3dSingleShowKeyPoint","true");
}
//显示最高点的夹角
-(void) U3dSingleShowHeightAngleWithIndex:(NSInteger)index
{
    //参数为数组下标_index  to string
    //    int _index=0;
    NSString* _string = [NSString stringWithFormat:@"%lu", (unsigned long)index];
    const char * _s =[_string UTF8String];
    UnitySendMessage("MsgReceiver","U3dSingleShowHeightAngle",_s);
}
//跳转单拍场景
-(void) U3dReturnToSingle
{
    UnitySendMessage("MsgReceiver","U3dReturnToSingle","");
}
// 切换到实时场景
- (void)U3dRealTimeMode
{
    UnitySendMessage("MsgReceiver","U3dReturnToMain", "");
}
//设置摄像机显示范围
-(void)SetCameraView//参数为float to string 范围0-1
{
    UnitySendMessage("MsgReceiver","SetCameraView","0.5");
}
//设置拖尾图片窗口（人物场景）//参数为string "true" “false”
-(void) U3dTrailWindowShow
{
    UnitySendMessage("MsgReceiver","U3dTrailWindowShow","true");
}
- (void)U3dTraningMode
{
    const char * _s = [@"" UTF8String];
    UnitySendMessage("MsgReceiver","U3dGetPracticeData", _s);
}

/**
 unity3D轨迹数据
 */
- (void)U3dTrainingWithJsonString:(NSString *)threeDJsonString
{
    const char * _s = [threeDJsonString UTF8String];
    UnitySendMessage("MsgReceiver", "U3dGetPracticeData", _s);
}



@end
